package main

import "fmt"

// 函数选项模式，大量引用
type DBOptions struct {
	Host     string
	Port     int
	UserName string
	Password string
	DBName   string
}

type Option func(*DBOptions)

func WithHost(host string) Option {
	return func(options *DBOptions) {
		options.Host = host
	}
}

func NewOps(options ...Option) *DBOptions {
	//先实例化好DBOptions，填充上默认值
	opts := &DBOptions{
		Host: "127.0.0.1",
		Port: 3306,
	}
	for _, opt := range options {
		opt(opts)
	}
	return opts
}

func main() {
	//opts := NewOps()
	opts := NewOps(WithHost("192.168.0.1"))
	fmt.Println(opts)

}
